/*
 * ClusteringForm.java
 *
 * Created on 14 2009, 21:19
 */
package gui;

import data.GeneralDefinitions.ClusteringPriorityParameters;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Frame;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import main.ToEngineFacade;

public class ClusteringForm extends javax.swing.JFrame {

    private Integer numOfClusters = null;
    ClusteringPriorityParameters priorityParam = null;
    private ToEngineFacade toEngineFacade;
    private Frame parentFrame;
    private Calendar endDate;
    private Calendar startDate;
    private SimpleDateFormat dateFrmt;

    /** Creates new form ClusteringForm */
    public ClusteringForm(Frame frame, ToEngineFacade toEngFacade) {
        setPreferredSize(new Dimension(635, 500));
        dateFrmt = new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss");
        initComponents();
        toEngineFacade = toEngFacade;
        parentFrame = frame;

        //default values for start and end Date
        startDate = Calendar.getInstance();
        endDate = Calendar.getInstance();
        endDate.add(Calendar.DAY_OF_MONTH, 7);
        startDateTextField.setText(dateFrmt.format(startDate.getTime()));
        endDateTextField.setText(dateFrmt.format(endDate.getTime()));

        //default values for params
       // numOfClusters = 100;
    //    numOfClustersTextField.setText(numOfClusters.toString());
    //    KBWrittenRadioButton.setSelected(true);
     //   priorityParam = ClusteringPriorityParameters.kbWritten;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        numOfClustersTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        KBWrittenRadioButton = new javax.swing.JRadioButton();
        KBReadRadioButton = new javax.swing.JRadioButton();
        readHitsRadioButton = new javax.swing.JRadioButton();
        writeHitsRadioButton = new javax.swing.JRadioButton();
        readRateRadioButton = new javax.swing.JRadioButton();
        writeRateRadioButton = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        startDateTextField = new javax.swing.JTextField();
        endDateTextField = new javax.swing.JTextField();
        changeStartDateAndTimeButton = new javax.swing.JButton();
        changeEndDateAndTimeButton = new javax.swing.JButton();
        back_jButton1 = new javax.swing.JButton();
        OK_jButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(3, 141, 187));
        jPanel1.setPreferredSize(new java.awt.Dimension(600, 453));

        jLabel1.setFont(new java.awt.Font("Bodoni MT Black", 1, 26));
        jLabel1.setForeground(new java.awt.Color(227, 186, 196));
        jLabel1.setText("Parameters for Clustring algorithm");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel2.setText("Num of Cluster");

        numOfClustersTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numOfClustersTextFieldActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel3.setText("Priority parameter");

        KBWrittenRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(KBWrittenRadioButton);
        KBWrittenRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        KBWrittenRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        KBWrittenRadioButton.setText("KB Written");
        KBWrittenRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KBWrittenRadioButtonActionPerformed(evt);
            }
        });

        KBReadRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(KBReadRadioButton);
        KBReadRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        KBReadRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        KBReadRadioButton.setText("KB Read");
        KBReadRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KBReadRadioButtonActionPerformed(evt);
            }
        });

        readHitsRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(readHitsRadioButton);
        readHitsRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        readHitsRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        readHitsRadioButton.setText("Read Cache Hits precentage");

        writeHitsRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(writeHitsRadioButton);
        writeHitsRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        writeHitsRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        writeHitsRadioButton.setText("Write Cache Hits precentage");

        readRateRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(readRateRadioButton);
        readRateRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        readRateRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        readRateRadioButton.setText("Read Data Rate");
        readRateRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                readRateRadioButtonActionPerformed(evt);
            }
        });

        writeRateRadioButton.setBackground(new java.awt.Color(3, 141, 187));
        buttonGroup1.add(writeRateRadioButton);
        writeRateRadioButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        writeRateRadioButton.setForeground(new java.awt.Color(255, 255, 255));
        writeRateRadioButton.setText("Write Data Rate");
        writeRateRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                writeRateRadioButtonActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel4.setText("Start Date and Time:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel5.setText("End Date and Time:");

        startDateTextField.setBackground(new java.awt.Color(3, 141, 187));
        startDateTextField.setEditable(false);
        startDateTextField.setFont(new java.awt.Font("Tahoma", 1, 18));
        startDateTextField.setForeground(new java.awt.Color(255, 255, 255));
        startDateTextField.setText("jTextField1");

        endDateTextField.setBackground(new java.awt.Color(3, 141, 187));
        endDateTextField.setEditable(false);
        endDateTextField.setFont(new java.awt.Font("Tahoma", 1, 18));
        endDateTextField.setForeground(new java.awt.Color(255, 255, 255));
        endDateTextField.setText("jTextField1");

        changeStartDateAndTimeButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        changeStartDateAndTimeButton.setForeground(new java.awt.Color(204, 0, 51));
        changeStartDateAndTimeButton.setText("Change Start");
        changeStartDateAndTimeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeStartDateAndTimeButtonActionPerformed(evt);
            }
        });

        changeEndDateAndTimeButton.setFont(new java.awt.Font("Tahoma", 1, 18));
        changeEndDateAndTimeButton.setForeground(new java.awt.Color(204, 0, 51));
        changeEndDateAndTimeButton.setText("Change End");
        changeEndDateAndTimeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeEndDateAndTimeButtonActionPerformed(evt);
            }
        });

        back_jButton1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        back_jButton1.setForeground(new java.awt.Color(57, 82, 148));
        back_jButton1.setText("Back");
        back_jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                back_jButton1ActionPerformed(evt);
            }
        });

        OK_jButton.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        OK_jButton.setForeground(new java.awt.Color(57, 82, 148));
        OK_jButton.setText("OK");
        OK_jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OK_jButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(endDateTextField)
                            .addComponent(startDateTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(changeEndDateAndTimeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(changeStartDateAndTimeButton, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(numOfClustersTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(KBWrittenRadioButton)
                            .addComponent(KBReadRadioButton)
                            .addComponent(writeRateRadioButton)
                            .addComponent(readRateRadioButton)
                            .addComponent(writeHitsRadioButton)
                            .addComponent(readHitsRadioButton)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(back_jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(OK_jButton))
                        .addComponent(jLabel1)))
                .addGap(38, 38, 38))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addGap(174, 174, 174))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(numOfClustersTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(KBWrittenRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(KBReadRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(readHitsRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(writeHitsRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(readRateRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(writeRateRadioButton)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(changeStartDateAndTimeButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(endDateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(changeEndDateAndTimeButton))
                .addGap(52, 52, 52)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OK_jButton)
                    .addComponent(back_jButton1))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void KBReadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KBReadRadioButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_KBReadRadioButtonActionPerformed

    private void readRateRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readRateRadioButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_readRateRadioButtonActionPerformed

    private void writeRateRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_writeRateRadioButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_writeRateRadioButtonActionPerformed

private void OK_jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OK_jButtonActionPerformed
    System.out.println(" OK Botton clicked ");
    boolean isAllParamOk = true;
    //price
    try {
        numOfClusters = new Integer(numOfClustersTextField.getText());
        KBWrittenRadioButton.requestFocus();
    } catch (NumberFormatException numberFormatException) {
        System.out.println("  Num Of Clusters mustbe an int !");
        JOptionPane.showConfirmDialog(
                null,
                "Num Of Clusters must be a number!",
                "Parameter type error",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.OK_OPTION);
        numOfClustersTextField.setText("");
        numOfClustersTextField.requestFocus();
        return;
    }

    //ordering parameter -    
    if (KBReadRadioButton.isSelected()) {
        priorityParam = ClusteringPriorityParameters.kbRead;
    } else if (KBWrittenRadioButton.isSelected()) {
        priorityParam = ClusteringPriorityParameters.kbWritten;
    } else if (readHitsRadioButton.isSelected()) {
        priorityParam = ClusteringPriorityParameters.ReadCacheHitsPrecentage;
    } else if (writeHitsRadioButton.isSelected()) {
        priorityParam = ClusteringPriorityParameters.WriteCahceHitsPrecentage;
    } else if (readRateRadioButton.isSelected()) {
        priorityParam = ClusteringPriorityParameters.numOfReads;
    } else if (writeRateRadioButton.isSelected()) {
        priorityParam = ClusteringPriorityParameters.numOfWrites;
    } else {
         JOptionPane.showConfirmDialog(
                null,
                "Ordering Parameter must be choosen!",
                "Parameter missing error",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.OK_OPTION);
         return;
      //  isAllParamOk = false;
    }

    //check if start date is after end date
    if (isAllParamOk && startDate.compareTo(endDate) > 0) { //start date is after end date
        isAllParamOk = false;
        JOptionPane.showConfirmDialog(
                null,
                "End date cannot be befor Start date!",
                "Parameters error",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.OK_OPTION);
        startDateTextField.requestFocus();
        return;
    }

    if (isAllParamOk) {
        toEngineFacade.computeClustering(numOfClusters, startDate, endDate, priorityParam);
        this.setVisible(false);
    } else {
        JOptionPane.showConfirmDialog(
                null,
                "You cannot continue if not all parameters are entered correctly!",
                "Missing parameters error",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.OK_OPTION);
    }
}//GEN-LAST:event_OK_jButtonActionPerformed

private void numOfClustersTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_numOfClustersTextFieldActionPerformed
    KBWrittenRadioButton.requestFocus();
}//GEN-LAST:event_numOfClustersTextFieldActionPerformed

private void changeEndDateAndTimeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeEndDateAndTimeButtonActionPerformed
    Calendar c = DateAndTimeSelector.showDateAndTimeSelector(this, "Date and Time Selector", startDate);
    //startDate = c;
    endDate = c;
    endDateTextField.setText(dateFrmt.format(c.getTime()));
}//GEN-LAST:event_changeEndDateAndTimeButtonActionPerformed

private void changeStartDateAndTimeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeStartDateAndTimeButtonActionPerformed
    Calendar c = DateAndTimeSelector.showDateAndTimeSelector(this, "Date and Time Selector", endDate);
    //endDate = c;
    startDate = c;
    startDateTextField.setText(dateFrmt.format(c.getTime()));
}//GEN-LAST:event_changeStartDateAndTimeButtonActionPerformed

private void back_jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_back_jButton1ActionPerformed
    parentFrame.setVisible(true);
    this.dispose();
}//GEN-LAST:event_back_jButton1ActionPerformed

private void KBWrittenRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KBWrittenRadioButtonActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_KBWrittenRadioButtonActionPerformed
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    public void run() {
    new ClusteringForm().setVisible(true);
    }
    });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton KBReadRadioButton;
    private javax.swing.JRadioButton KBWrittenRadioButton;
    private javax.swing.JButton OK_jButton;
    private javax.swing.JButton back_jButton1;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton changeEndDateAndTimeButton;
    private javax.swing.JButton changeStartDateAndTimeButton;
    private javax.swing.JTextField endDateTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField numOfClustersTextField;
    private javax.swing.JRadioButton readHitsRadioButton;
    private javax.swing.JRadioButton readRateRadioButton;
    private javax.swing.JTextField startDateTextField;
    private javax.swing.JRadioButton writeHitsRadioButton;
    private javax.swing.JRadioButton writeRateRadioButton;
    // End of variables declaration//GEN-END:variables
}
